#include "init_server.h"

void init_server_error( const char *message)
{
	fputs(message, stderr);
	fputc('\n', stderr);
	exit(1);
}

int init_server( int& serv_sock, struct sockaddr_in& serv_addr, char **argv)
{
	serv_sock = socket(PF_INET, SOCK_STREAM, 0);

	if(serv_sock == -1)
		init_server_error("socket() error");

	memset(&serv_addr, 0, sizeof(serv_addr));
	serv_addr.sin_family = AF_INET;
	serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
	serv_addr.sin_port = htons(atoi(argv[1]));

	if(bind(serv_sock, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) == -1)
		init_server_error("bind() error");

	if(listen(serv_sock, 20) == -1)
		init_server_error("listen() error");

	return 0;
}
